package com.res.share.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.res.share.model.ResCollection;
import com.res.share.model.ResFollow;
import com.res.share.mapper.ResFollowMapper;
import com.res.share.model.dto.UserFollowModel;
import com.res.share.service.IResFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户关注 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-22
 */
@Service
public class ResFollowServiceImpl extends ServiceImpl<ResFollowMapper, ResFollow> implements IResFollowService {

    @Override
    public List<Long> findUserResFollowList(Integer beLongUserId, Integer type) {
        return baseMapper.findUserResFollowList(beLongUserId, type);
    }

    /**
     * @param userId           我要关注
     * @param followMeBelongId 要专注的人
     * @param type
     * @return
     */
    @Override
    public Integer editFollowMe(Integer userId, Integer followMeBelongId, Integer type) {
        Integer result = 0;
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("USER_ID", userId);
        queryWrapper.eq("FOLLOW_USER_ID", followMeBelongId);
        queryWrapper.eq("IS_DEL", 0);
        ResFollow resFollow = baseMapper.selectOne(queryWrapper);
        if (resFollow == null) {
            resFollow = new ResFollow();
            resFollow.setFollowUserId(followMeBelongId);
            resFollow.setUserId(userId);
            resFollow.setCreateUserId(userId);
            resFollow.setStatus(type);
            int insert = baseMapper.insert(resFollow);
            if (insert > 0) {
                result = 1;
            }
        } else {
            Integer status = resFollow.getStatus();
            if (status == 1) {
                result = 2;
            } else {
                resFollow.setStatus(type);
                int insert = baseMapper.updateById(resFollow);
                if (insert > 0) {
                    result = 1;
                }
            }
        }
        return result;
    }

    @Override
    public IPage<UserFollowModel> mylikeuserList(Page<UserFollowModel> page, Integer userId) {
        return baseMapper.mylikeuserList(page,userId);
    }

    @Override
    public IPage<UserFollowModel> likemeDetailList(Page<UserFollowModel> page, Integer userId) {
        return baseMapper.likemeDetailList(page,userId);
    }

    @Override
    public void cancelLikeUser(String followUserId, Integer userId) {
        baseMapper.cancelLikeUser(followUserId,userId);
    }

    @Override
    public IPage<ResCollection> mylikeuserresList(Page<ResCollection> page, Integer userId) {
        return baseMapper.mylikeuserResList(page,userId);
    }
}
